package com.cfg.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cfg.domain.DmCustomstatsCol;
import com.cfg.mapper.DmCustomstatsColMapper;
import com.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class DmCustomstatsColService extends ServiceImpl<DmCustomstatsColMapper, DmCustomstatsCol> {


    public List<DmCustomstatsCol> buildDeptTreeSelect(DmCustomstatsCol dmCustomstatsCol)
    {
        List<DmCustomstatsCol> dmCustomstatsList=new ArrayList<>();
       if (dmCustomstatsCol.getColumnTYPE()=="ALL"){
             dmCustomstatsList=this.list();
       }
       else {
           dmCustomstatsList=getListByDmCustomstatsCol(dmCustomstatsCol);
       }

        List<DmCustomstatsCol> deptTrees = buildDeptTree(dmCustomstatsList);
        return  deptTrees;

    }

    public List<DmCustomstatsCol>getListByDmCustomstatsCol(DmCustomstatsCol dmCustomstatsCol){
        QueryWrapper<DmCustomstatsCol>dmCustomstatsColQueryWrapper=new QueryWrapper<>();
        if(dmCustomstatsCol.getColumnTYPE().equals("SUBJECT")){
            QueryWrapper<DmCustomstatsCol> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("NAME_LOCAL", "ACADEMIC");
            DmCustomstatsCol dmCustomstatsCol1 = this.getOne(queryWrapper);
            if (dmCustomstatsCol1 != null) {
                dmCustomstatsColQueryWrapper.ne("UP_ID", dmCustomstatsCol1.getId()).or()
                        .isNull("UP_ID").ne("id",dmCustomstatsCol1.getId());
            }
        }
        else {
            QueryWrapper<DmCustomstatsCol> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("NAME_LOCAL", dmCustomstatsCol.getColumnTYPE());
            DmCustomstatsCol dmCustomstatsCol1 = this.getOne(queryWrapper);

            if (dmCustomstatsCol1 != null) {
                dmCustomstatsColQueryWrapper.eq("ID", dmCustomstatsCol1.getId()).or()
                        .eq("UP_ID", dmCustomstatsCol1.getId());

            }
        }
        return this.list(dmCustomstatsColQueryWrapper);
    }

    public List<DmCustomstatsCol> buildDeptTree(List<DmCustomstatsCol> depts)
    {
        List<DmCustomstatsCol> returnList = new ArrayList<DmCustomstatsCol>();
        List<String> tempList = depts.stream().map(DmCustomstatsCol::getId).collect(Collectors.toList());
        for (DmCustomstatsCol dept : depts)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getUpID()))
            {
                recursionFn(depts, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = depts;
        }
        return returnList;
    }
    private void recursionFn(List<DmCustomstatsCol> list, DmCustomstatsCol t)
    {
        // 得到子节点列表
        List<DmCustomstatsCol> childList = getChildList(list, t);
        t.setChildren(childList);
        for (DmCustomstatsCol tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<DmCustomstatsCol> getChildList(List<DmCustomstatsCol> list, DmCustomstatsCol t)
    {
        List<DmCustomstatsCol> tlist = new ArrayList<DmCustomstatsCol>();
        Iterator<DmCustomstatsCol> it = list.iterator();
        while (it.hasNext())
        {
            DmCustomstatsCol n = (DmCustomstatsCol) it.next();
            if (StringUtils.isNotNull(n.getUpID()) && n.getUpID().equals(t.getId()) )
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<DmCustomstatsCol> list, DmCustomstatsCol t)
    {
        return getChildList(list, t).size() > 0;
    }

}
